package com.lianyu507.service.impl;

import com.lianyu507.mapper.MbManagerMapper;
import com.lianyu507.pojo.MbManager;
import com.lianyu507.pojo.MbManagerExample;
import com.lianyu507.service.ManagerService;
import com.lianyu507.util.MD5;
import com.lianyu507.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ManagerSericeImpl implements ManagerService{


    @Autowired
    private MbManagerMapper mbManagerMapper;

    @Override
    public boolean afterLogin(String managerName, String managerPassword) {
        MbManagerExample example = new MbManagerExample();
        MbManagerExample.Criteria criteria = example.createCriteria();
        criteria.andManagerNameEqualTo(managerName);
        List<MbManager> list = mbManagerMapper.selectByExample(example);
        if (list != null && list.get(0).getStatus() == 1) {
            if (!list.get(0).getManagerPassword().equals(MD5.encode(managerPassword))) {
                throw new RuntimeException("密码错误");
            }
           //把用户信息写入session

           /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            HttpSession session = request.getSession();
            session.setAttribute("managerName", managerName);*/
            UserContext.putCurrentManager(list.get(0));
            return true;

        } else {
            throw new RuntimeException("用户不存在");
        }
    }




}
